package com.QSystem.service;

import com.QSystem.mapper.AnswerMapper;
import com.QSystem.mapper.DetailMapper;
import com.QSystem.mapper.SurveyMapper;
import com.QSystem.mapper.TitleMapper;
import com.QSystem.pojo.*;
import com.QSystem.util.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

@Service
public class AnswerService {

    @Autowired
    SurveyMapper surveyMapper;
    @Autowired
    TitleService titleService;
    @Autowired
    AnswerMapper answerMapper;
    @Autowired
    DetailMapper detailMapper;
    @Autowired
    TitleMapper titleMapper;

    Map<String,Object> map=new HashMap<>();

    //获取单个survey
    public Result getSurvey(Integer surId){
        Survey survey=new Survey();
        //根据surid获取survey
        survey=surveyMapper.queryBySurid(surId);

        //如果不存在该问卷，返回错误信息
        if(survey==null){
            System.out.println("不存在该问卷");
            return Result.error().message("不存在该问卷");
        }
        //如果状态不为已发布，拒绝访问
        else if(survey.getState()!=2){
            System.out.println("该问卷尚未发布");
            return  Result.error().message("该问卷尚未发布");
        }
        //状态为已发布，并且存在，返回survey的信息
        else{
            //然后封装到wholesurvey中
            WholeSurvey wholeSurvey=new WholeSurvey();
            wholeSurvey.setSurvey(survey);
            System.out.println("当前的wholesurvey1"+wholeSurvey);
            //wholesurvey要获取所有的wholetitle
            wholeSurvey=titleService.queryBySurid(wholeSurvey);
            System.out.println("当前的wholesurvey2"+wholeSurvey);
            map.put("WholeSurvey",wholeSurvey);
            return Result.ok().data(map);
        }

    }

    //存储wholeanswer,添加answer以及detail
    public Result addAnswer(WholeAnswer wholeAnswer){
        //首先应该找到对应的survey，然后答卷数加一（待做）
        Integer surId=wholeAnswer.getAnswer().getSurId();
        surveyMapper.updateNum(surId);
        //然后添加相应的答卷,并且获取到自增的ansId
        answerMapper.add(wholeAnswer.getAnswer());
        Integer ansId=wholeAnswer.getAnswer().getAnsId();
        System.out.println(wholeAnswer.getAnswer());
        //插入details
        for (Detail detail : wholeAnswer.getDetails()) {
            detail.setAnsId(ansId);
            System.out.println("detail:"+detail);
            detailMapper.add(detail);
        }
        return Result.ok().message("答卷提交成功");

    }


    //获取某问卷的答题情况
    public Result getAnswer(Integer surId){
        //定义变量
        List<WholeAnswer> wholeAnswers=new LinkedList<>();

        List<Answer> answers=new LinkedList<>();

        //获取答卷
        answers=answerMapper.getBySurId(surId);
        //System.out.println(answers);
        //封装wholeanswer

        for (Answer answer : answers) {
            WholeAnswer wholeAnswer=new WholeAnswer();
            wholeAnswer.setAnswer(answer);
            Integer ansId=answer.getAnsId();//获取当前ansId
            //根据ansId获取所有detail
            wholeAnswer.setDetails( detailMapper.getByAnsId(ansId));
            //存到wholeanswers中
            wholeAnswers.add(wholeAnswer);
        }

        return Result.ok().data("wholeAnswers",wholeAnswers);
       // System.out.println("封装后的wholeanswers::"+wholeAnswers);
    }

    public  Result gettitle(Integer surId){
        return  Result.ok().data("title",titleMapper.queryBySurid(surId));
    }

}
